// 此文件为数组版，即多种语言放再一个文件

const fs = require("fs")
const path = require('path')
const file = path.resolve(__dirname, './文字.txt')
const text = require("./messages.js")
const axios = require('axios')
const md5 = require('md5')
const qs = require('qs')
// &from=&to=en&text=考勤计算

function waitTime(time = 1500){
    return new Promise((res) => {
        setTimeout(() => {
            res()
        }, time);
    })
}

let url = 'https://www.iciba.com/word?w='

// url = `https://fanyi.youdao.com/translate?&doctype=json&type=cn&i=`
url = `http://fanyi.youdao.com/translate?&doctype=json&type=ZH_CN2EN&i=`
url = `http://translate.google.com/translate_a/single?client=t&dt=t&dj=1&ie=UTF-8&sl=en&tl=zh-tw&q=`

const translatee = require('google-translate-cn-api')

// 翻译
function translate(word){
    return new Promise(async (res, rej) => {
        // axios.get(url+word).then(e => {
        translatee(word, {to: 'zh-tw'}).then(e => {
            res(e.text)
        }).catch(err => {
            console.log(err)
            rej(err)
        })
    })
}


// 递归函数
function revursion(node){
    for(let k in node){
        if(typeof node[k] == "object"){
            fs.writeFile(file, '\n' + '\n' + k + '\n' + '\n',{flag: 'a'},err => {})
            revursion(node[k])
        }else{
            translate(node[k])
            fs.writeFile(file, node[k] + '\n',{flag: 'a'},err => {})
        }
    }
}

// 一个文件一个语言翻译
async function deepTranslate(node){
    for(let k in node){
        if(typeof node[k] == "object"){
            await deepTranslate(node[k])
        }else{
            await waitTime(1000)
            await translate(node[k])
        }
    }
}

let a = 1
let b = 0

// 一个文件2种语言翻译
async function deepTranslate2(node){
    let res = node.constructor === Array ? [] : {};
    for(let k in node){
        if(node[k] instanceof Array){
            res[k] = []
            res[k][a] = node[k][a]
            res[k][b] = await translate(node[k][a])
            console.log(res[k][a], res[k][b])
        }else if(Object.prototype.toString.call(node) === '[object Object]'){
            res[k] = await deepTranslate2(node[k])
        }else{
            console.log('出现不是数组也不是对象类型！！！')
        }
    }
    return res
};

//获取dom元素下的结果
function getDom(doc, word){
    let reg = /<p>(.*?)<\/p>/img
    let res = reg.exec(doc)
    if(res instanceof Array){
        return res[1]
    }else {
        return
        reg = /<sapn class="jsx-1885187966" .*?>(.*?)<\/span>/img
        res = reg.exec(doc)
        console.log('二次查询', res)
        if(res instanceof Array){
            // return res[1]
        }else {
            console.log(word, '翻译出错！！！')
        }
    }
}
// 百度翻译
async function baiduFY(){
    let url = `https://fanyi-api.baidu.com/api/trans/vip/translate` //百度
    let data = { 
        q: '哈哈',
        from: 'zh',
        to: 'en',
        appid: 20220103001045602,
        salt: 10,
     }
     data.sign =  md5(data.appid + data.q + data.salt+ 's930LazPVwA9yC8I3XCy')
     console.log( data.sign)
   let rs = await axios({
       type: 'GET',
       data: qs.stringify(data),
       url,
       headers: { 'content-type': 'application/x-www-form-urlencoded' },
   })
}
(async () => {
    let res  = await deepTranslate2(text)
    console.log(res, '最终结果')
    fs.writeFile(`./res/result${Date.now()}.json`, JSON.stringify(res), err => {
        if(err){
            console.log('写入文件失败')
        }else{
            console.log('写入文件成功')
        }
    })
})();